<template>
  <a-spin :loading="loading" style="width: 100%">
    <a-card
      class="general-card"
      :header-style="{ paddingBottom: '0' }"
      :body-style="{ padding: '20px' }">
      <Chart height="350px" :option="chartOption" />
    </a-card>
  </a-spin>
</template>

<script lang="ts" setup>
  import useLoading from '@/hooks/loading';
  import useChartOption from '@/hooks/chart-option';
  import { ref, watch } from 'vue';
  let props = defineProps({
      data:{
        type: Array,
        default: ()=>[]
      }
  });

  let unConnet = ref(0);
  let conneted = ref(0);
  let unacquired = ref(0);
  let acquired = ref(0);

  watch(()=> props.data, (value)=>{
    value.forEach((item: any)=>{
       if(item.type == '未接通'){
        unConnet.value = item?.value || 0;
       } else if(item.type == '已接通'){
        conneted.value = item?.value || 0;
       } else if(item.type == '未获取'){
        unacquired.value = item?.value || 0;
       } else if(item.type == '已获取'){
        acquired.value = item?.value || 0;
       }
    })
  },{
    deep: true,
    immediate: true
  })

  
  const { loading } = useLoading();
  const { chartOption } = useChartOption((isDark) => {
    isDark = false;
    return {
      legend: {
        left: 'center',
        data: ['未接','接通'],
        bottom: 0,
        icon: 'circle',
        itemWidth: 8,
        textStyle: {
          color: isDark ? 'rgba(255, 255, 255, 0.7)' : '#4E5969',
        },
        itemStyle: {
          borderWidth: 0,
        },
      },
      tooltip: {
        show: true,
        trigger: 'item',
      },
      graphic: {
        elements: [
          {
            type: 'text',
            left: 'center',
            top: '40%',
            style: {
              text: '',
              textAlign: 'center',
              fill: '#333333',
              fontSize: 14,
            },
          },
          {
            type: 'text',
            left: 'center',
            top: '50%',
            style: {
              textAlign: 'center',
              fill: '#333333',
              fontSize: 16,
              fontWeight: 500,
            },
          },
        ],
      },
      series: [
        {
          type: 'pie',
          radius: ['50%', '70%'],
          center: ['50%', '50%'],
          label: {
            formatter: '{d}%',
            fontSize: 14,
            color: isDark ? 'rgba(255, 255, 255, 0.7)' : '#4E5969',
          },
          itemStyle: {
            borderColor: isDark ? '#232324' : '#fff',
            borderWidth: 1,
          },
          data: [
            {
              value: unConnet.value,
              name: '未接',
              itemStyle: {
                color: '#5939DC',
              },
            },
            {
              value: conneted.value,
              name: '接通',
              itemStyle: {
                color: '#5FC890',
              },
            },
          ],
        },
      ],
    };
  });
</script>

<style scoped lang="less"></style>
